<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;
use DB;
use App\Http\Requests;
use App\Http\Controllers\Controller;

class CommentController extends Controller
{
    /**
    *   显示商品评价页面
    */
    public function getIndex(Request $request)
    {
        $res = DB::table('project_goods_comment')
        ->join('project_goods','project_goods_comment.gid','=','project_goods.id')
        ->join('project_user','project_goods_comment.uid','=','project_user.id')
        ->select('project_goods_comment.*','project_user.username','project_goods.goodspic','project_goods.goodsname')
        ->orWhere('goodsname','like','%'.$request->input('search').'%')
        ->where('project_goods_comment.status','0')
        ->paginate($request->input('num',5));

        return view('admin/comment/index',['res'=>$res,'request'=>$request->all()]);
    }

    /**
    *   添加商品评论的页面
    */
    public function getAdd(Request $request)
    {
        //后台管理员
        $row = DB::table('project_admin_user')->get();

        //获取分类的数据
        $res = DB::table('project_type')
        ->select(DB::raw('*,concat(path,id) as paths'))
        ->orderby('paths')
        ->get();

        foreach($res as $k=>$v) {
            //以逗号进行分割
            $path = explode(',',$v->path);
            //获取逗号的数量
            $len = count($path)-2;
            //根据逗号的数量进行重复
            $v->typename = str_repeat('--|',$len).$v->typename;
        }

        return view('admin/comment/add',['res'=>$res,'row'=>$row]);
    }

    /**
    *   ajax处理商品名称
    */
    public function getAjax(Request $request)
    {
        $id = $_GET['id'];

       $res = DB::table('project_goods')->where('tid',$id)->get();

       echo json_encode($res);
    }

    /**
    *   ajaxajax处理商品内容 要图片
    */
    public function getAjaxajax(Request $request)
    {
        $id = $_GET['id'];

        $res = DB::table('project_goods')->where('id',$id)->first();

        echo $res->goodspic;
    }

    /**
    *   添加商品评论
    */
    public function postInsert(Request $request)
    {
        $res = $request->except('_token','tid');

        $row = DB::table('project_admin_user')->where('id',$request->input('uid'))->first();

        $res['username'] = $row->username;
        $res['posttime'] = time();
       
        $row = DB::table('project_goods_comment')->insert($res);

        if($row) {

            return redirect("/Admin/comment/index")->with("success","添加成功");
        } else {
            return back()->with("error","添加失败");
        }
    }

    /**
    *   修改评论页面
    */
    public function getEdit(Request $request)
    {
        $id = $request->input('id');

        //查询
       $res = DB::table('project_goods_comment')->where('id',$id)->first();

       $row = DB::table('project_goods')->where('id',$res->gid)->first();

       return view('admin/comment/edit',['res'=>$res,'row'=>$row]);
    }

    /**
    *   修改评论方法
    */
    public function postUpdate(Request $request)
    {
        $res = $request->except('_token','id','goodsname');

        $id = $request->input("id");

       $row = DB::table('project_goods_comment')->where('id',$id)->update($res);

       if($row) {

            return redirect('/Admin/comment/index')->with("success","修改成功");
       } else {

            return back()->with("error","未做任何修改");
       }

    }

    /**
    *    回复评论
    */
    public function postAjax(Request $request)
    {
         $id = $_POST['id'];

         $arr['reply'] = $_POST['reply'];
 
         $res = DB::table('project_goods_comment')->where('id',$id)->update($arr);

         if($res) {

            echo "yes";
         } else {

            echo "no";
         }
    }

    /**
    *   禁用品论
    */
    public function getDel(Request $request)
    {
        $id = $_GET['id'];

        $arr['status'] = 1;

        $row = DB::table('project_goods_comment')->where('id',$id)->update($arr);

        if($row) {
            echo "yes";
        } else {
            echo "no";
        }
    }

    /**
    *   评论回收站
    */
    public function getRecycle(Request $request)
    {

         $res = DB::table('project_goods_comment')
        ->join('project_goods','project_goods_comment.gid','=','project_goods.id')
        ->select('project_goods_comment.*','project_goods.goodspic','project_goods.goodsname')
        ->orWhere('goodsname','like','%'.$request->input('search').'%')
        ->where('project_goods_comment.status',1)
        ->paginate($request->input('num',5));

         return view('admin/comment/recycle',['res'=>$res,'request'=>$request->all()]);
    }

    /**
    *   启用品论
    */
    public function getEnable(Request $request)
    {
        $id = $_GET['id'];

        $arr['status'] = 0;

        $row = DB::table('project_goods_comment')->where('id',$id)->update($arr);

        if($row) {
            echo "yes";
        } else {
            echo "no";
        }
    }


















    













}
